package sdu.attendancechecker.services;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import sdu.attendancechecker.dto.*;
import sdu.attendancechecker.models.TeacherModel;

public class AdminService {

	/**
	 * 
	 * @return
	 */
	public static Admin login(String email, String password) {
		Connection conn = DbService.getConnection();
		if (conn == null) {
			return null;
		}
		String sql = "SELECT * FROM admin WHERE email = ? AND password = ?";
		try {
			PreparedStatement pstmnt = conn.prepareStatement(sql);
			pstmnt.setString(1, email);
			pstmnt.setString(2, password);
			ResultSet result = pstmnt.executeQuery();
			if (result.next() == false)
				return null;
			Admin admin = new Admin();
			admin.setEmail(email);
			admin.setId(result.getInt("admin_id"));
			return admin;

		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}

	}

	/**
	 * 
	 * @return
	 */
	public static void logout() {
		// TODO - implement {class}.{operation}
		throw new UnsupportedOperationException();
	}

	/**
	 * 
	 * @return
	 */
	public static void forgotPassword() {
		// TODO - implement {class}.{operation}
		throw new UnsupportedOperationException();
	}

	/**
	 * 
	 * @return
	 */
	public static boolean addTeacher(Integer id, String name, String surname,
			String email) {
		if (id == null || name == null || name.length() == 0 || surname == null
				|| surname.length() == 0 || email == null
				|| email.length() == 0)
			return false;
		String password = PasswordService.generatePassword();
		return TeacherModel.add(id, name, surname, password, email);
	}

	/**
	 * 
	 * @return
	 */
	public static void updateTeacher() {
		// TODO - implement {class}.{operation}
		throw new UnsupportedOperationException();
	}

	/**
	 * 
	 * @return
	 */
	public static boolean deleteTeacherById(Integer id) {
		if (id == null)
			return false;
		Connection conn = DbService.getConnection();
		if (conn == null)
			return false;
		String sql = "DELETE FROM question WHERE teacher_id = ?";
		try {
			PreparedStatement pstmnt = conn.prepareStatement(sql);
			pstmnt.setInt(1, id);
			pstmnt.executeUpdate();
			
		} catch (SQLException e) {
			e.printStackTrace();
			
		}
		
		return TeacherModel.delete(id);
	}

	/**
	 * 
	 * @return
	 */
	public static boolean answerQuestion(int id, String answer) {
		Connection conn = DbService.getConnection();
		if (conn == null)
			return false;
		String sql = "UPDATE question set answer = ? WHERE question_id = ?";
		try {
			PreparedStatement pstmnt = conn.prepareStatement(sql);
			pstmnt.setString(1, answer);
			pstmnt.setInt(2, id);
			pstmnt.executeUpdate();
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}

	/**
	 * 
	 * @return
	 */
	public static void updateQuestion() {
		// TODO - implement {class}.{operation}
		throw new UnsupportedOperationException();
	}

	/**
	 * 
	 * @return
	 */
	public static void deleteQuestion() {
		// TODO - implement {class}.{operation}
		throw new UnsupportedOperationException();
	}

	/**
	 * 
	 * @return
	 */
	public static void importDb() {
		// TODO - implement {class}.{operation}
		throw new UnsupportedOperationException();
	}

	public static ArrayList<Teacher> getTeachers() {
		return TeacherModel.getAll();
	}

	public static ArrayList<Question> getQuestions() {
		Connection conn = DbService.getConnection();
		if (conn == null)
			return null;
		String sql = "SELECT question_id, teacher_id, question, answer FROM question";
		ArrayList<Question> questionList = new ArrayList<Question>();

		Statement stmnt = null;
		Statement stmnt2 = null;
		try {
			stmnt = conn.createStatement();
			ResultSet result = stmnt.executeQuery(sql);
			while (result.next()) {
				Question q = new Question();
				q.setQuestion_id(result.getInt("question_id"));
				q.setQuestion(result.getString("question"));
				q.setTeacher_id(result.getInt("teacher_id"));
				q.setAnswer(result.getString("answer"));
				questionList.add(q);
			}
			for (Question temp : questionList) {
				stmnt = conn.createStatement();
				result = stmnt
						.executeQuery("SELECT name,surname FROM teacher WHERE id = "
								+ temp.getTeacher_id());
				result.next();
				temp.setTeacher_name(result.getString("name") + " "
						+ result.getString("surname"));
			}

			return questionList;
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}

	}

	public static boolean deleteQuestionById(int id) {
		Connection conn = DbService.getConnection();
		if (conn == null)
			return false;
		String sql = "DELETE FROM question WHERE question_id = ?";
		try {
			PreparedStatement pstmnt = conn.prepareStatement(sql);
			pstmnt.setInt(1, id);
			pstmnt.executeUpdate();
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}

	public static Question getQuestionById(int question_id) {
		ArrayList<Question> questions = AdminService.getQuestions();
		for (Question q : questions) {
			if (q.getQuestion_id() == question_id)
				return q;
		}
		return null;
	}

	public static boolean updatePasswordByEmail(String email, String pwd) {
		Connection conn = DbService.getConnection();
		if (conn == null)
			return false;
		String sql = "UPDATE admin SET password = ? WHERE email = ?";
		try {
			PreparedStatement pstmnt = conn.prepareStatement(sql);
			pstmnt.setString(2, email);
			pstmnt.setString(1, pwd);
			pstmnt.executeUpdate();
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
		
	}

}